package com.practice.backend.Common;

import com.practice.backend.Entity.User;
import io.jsonwebtoken.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;




@Setter
@Getter
@Component
public class Jwt {


    private String token;
    public static String createJwt(User user){
        String key = "JwtTokenKey";
        JwtBuilder jwtBuilder = Jwts.builder();
        return jwtBuilder.setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .claim("currentUserId",user.getId())
                .setSubject("currentUserId")
                .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))
                .setId(UUID.randomUUID().toString())
                .signWith(SignatureAlgorithm.HS256, key)
                .compact();
    }
    public static Integer parseToken(String token){
        JwtParser jwtParser = Jwts.parser();
        Claims claims = jwtParser.setSigningKey("JwtTokenKey").parseClaimsJws(token).getBody();
        System.out.println( claims.get("currentUserId"));
        return (Integer) claims.get("currentUserId");
    }


}
